package com.opteron.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opteron.model.UserInfo;
import com.opteron.service.UserService;

@Controller
public class LoginAction extends BasesAction implements SessionAware {

	private static final long serialVersionUID = -2470999058253838327L;
	
	private Map<String, Object> session;
	@Autowired
	private UserService userService;
	private InputStream inputStream;
	private String imageFileName;
	private List<Object[]> testList;
	private List<Object> testList2;
	
	public String login() {
		session.clear();
		List<UserInfo> userList = (List<UserInfo>)userService.getAll();
		System.out.println(userList);
		return LOGIN;
	}

	public String validateUser() {
		String code = getSession().getAttribute("validateCode").toString();
		String signId = getParameter("signId");
		String password = getParameter("password");
		UserInfo user = (UserInfo)userService.getByUnique("signId", signId);
		if(user.getPassword().equals(password)) {
			session.put("USER", user);
			UserInfo u = (UserInfo)session.get("USER");
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
	
	public String logout() {
		
		session.clear();
		return SUCCESS;
	}
	
	public String gMap() {
		return SUCCESS;
	}
	
	public String validateCode() {
		int CODE_LETTER_LENGTH = Math.round((float) 102 / (float) 6);
		String[] s ={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
		String code="";
		
		Random random = new Random();
		BufferedImage image = new BufferedImage(102, 32, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		
		try {
			g.setColor(getRandColor(200, 250));	// 設定背景顏色
			g.fillRect(0, 0, 102, 32);
			g.setFont(new Font("Lucida Console", Font.HANGING_BASELINE, 26));	// 字體
			g.setColor(getRandColor(160, 200));
			for (int i = 0; i < 300; i++) {
				int x = random.nextInt(102);
				int y = random.nextInt(32);
				int xl = random.nextInt(30);
				int yl = random.nextInt(30);
				g.drawLine(x, y, x + xl, y + yl);
			}
			for(int i=0; i<6;i++) {
				int j = random.nextInt(32 / 4);
				String s1 =  s[(int)(s.length * Math.random())];
				g.setColor(new Color( 20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
				g.drawString(s1, CODE_LETTER_LENGTH * i + 1, 32 * 3/4  - j);
				code += s1;
			}
			System.out.println(code);
			getSession().setAttribute("validateCode", code);
			g.dispose();
			String randomNum = String.valueOf(random.nextInt());
			imageFileName = "imageCode_".concat(randomNum).concat(".jpeg");
			
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			ImageIO.write(image, "JPEG", os);
			setInputStream(new ByteArrayInputStream(os.toByteArray()));
		} catch(Exception e) {
			System.out.print(e);
		}
		return "codeImage";
	}
	
	private Color getRandColor(int fc, int bc) {
		Random random = new Random();
		fc = (fc > 255) ? 255 : fc;
		bc = (bc > 255) ? 255 : bc;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}
	
	public String queryUser() {
		this.pushData("userList", userService.getAll());
		return SUCCESS;
	}
	
	public String testHtml5() {
		return "test";
	}
	
	public void test1() {
		String id = getRequest().getParameter("id");
		System.out.println(id);
	}
	
	public String test() {
		String[] s = getRequest().getParameterValues("address");
		System.out.print(s[0]);
		return SUCCESS;
	}
	
	public String testList() {
		HttpServletRequest request = ServletActionContext.getRequest();
		try{
			Map<String, String[]> s = request.getParameterMap();
			String[] s2 = null;
			for(String s1 : s.keySet()) {
				s2 = s.get(s1);
			}
			System.out.println(s);
			System.out.println(s2);
			request.setAttribute("json", "holle");
		}catch(Exception e) {
			System.out.println(e);
		}
		return SUCCESS;
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public List<Object[]> getTestList() {
		return testList;
	}

	public void setTestList(List<Object[]> testList) {
		this.testList = testList;
	}

	public List<Object> getTestList2() {
		return testList2;
	}

	public void setTestList2(List<Object> testList2) {
		this.testList2 = testList2;
	}
}
